<%
Function GCD(FirstNum, SecondNum)
	if FirstNum > SecondNum then
		X = CDbl(FirstNum)
		Y = CDbl(SecondNum)
	else
		Y = CDbl(FirstNum)
		X = CDbl(SecondNum)
	end if
	
	EndNow = 0
	Do While EndNow = 0
		R = CDbl(X MOD Y)			'R = Remainder
		If R = 0 then
			GCD = CDbl(Y)
			EndNow = 1
		End If
		X = CDbl(Y)
		Y = CDbl(R)  
	Loop
End Function

Function IsPrime(ValueToTest)

   Dim Lim
   Dim i
   
   i=CDbl(0)
   
   IsPrime = CBool(1)
   
   Lim = CDbl(Sqr(ValueToTest))
   
   For i = 2 To Lim
   
      If ValueToTest Mod i = 0 Then
         IsPrime = CBool(0)
         Exit For
      End If
      
   Next

End Function

Function ModFunction(Main,Modulus)
	ModFunction = CDbl( Main - (Int(Main / Modulus) * Modulus) )
End Function

Function Mult(ByVal x , ByVal p , ByVal m )
    y = 1
    
    Do While p > 0

        Do While (p / 2) = Int((p / 2))
            x = ModFunction((x * x), m)
            p = p / 2
        Loop

        y = ModFunction((x * y), m)
        p = p - 1
		
    Loop
	
    Mult = y

End Function

Function Euler(E3, PHI3)
	Dim u1
	Dim u2
	Dim u3
	Dim v1
	Dim v2
	Dim v3
	Dim q
	Dim t1
	Dim t2
	Dim t3
	Dim z
	Dim uu
	Dim vv
	Dim inverse
	
	u1 = 1
	u2 = 0
	u3 = PHI3
	v1 = 0
	v2 = 1
	v3 = E3
	
	Do Until (v3 = 0)
	     q = Int(u3 / v3)
	     t1 = u1 - q * v1
	     t2 = u2 - q * v2
	     t3 = u3 - q * v3
	
	     u1 = v1
	     u2 = v2
	     u3 = v3
	
	     v1 = t1
	     v2 = t2
	     v3 = t3
	     z = 1
	Loop
	
	uu = u1
	vv = u2
	
	If (vv < 0) Then
	     inverse = vv + PHI3
	Else
	     inverse = vv
	End If
	
	Euler = inverse
	
End Function

Function Encrypt(MyString,Encryption,Modulus)

Dim lgCount

For lgCount = 1 to Len(MyString)
	 Encrypt = Encrypt & Mult(CLng(Asc(Mid(MyString, lgCount, 1))), Encryption, Modulus) & ","
Next

End Function

Dim intCount
Dim P
Dim Q
Dim N
Dim DN
Dim E
Dim D
Dim PrimeLowLimit
Dim PrimeHighLimit
Dim EncryptionLowLimit
Dim EncryptionHighLimit

intCount = CDbl(0)

PrimeLowLimit = int(3170)
PrimeHighLimit = int(9999)

Randomize

P = CInt( PrimeLowLimit + ( PrimeHighLimit - PrimeLowLimit ) * Rnd )
Q = CInt( PrimeLowLimit + ( PrimeHighLimit - PrimeLowLimit ) * Rnd )

Do while IsPrime(P) = False
	P = CInt( PrimeLowLimit + ( PrimeHighLimit - PrimeLowLimit ) * Rnd )
Loop

Do while IsPrime(Q) = False
	Q = CInt( PrimeLowLimit + ( PrimeHighLimit - PrimeLowLimit ) * Rnd )
Loop

N = CDbl(P * Q)
DN = CDbl((P - 1)*(Q - 1))

EncryptionLowLimit = Cdbl( 10000000 )
EncryptionHighLimit = Cdbl( 99999999 )

E = Int( EncryptionLowLimit + ( DN - EncryptionLowLimit ) * Rnd )

Do While GCD(E,DN) <> 1
	E = Int( EncryptionLowLimit + ( DN - EncryptionLowLimit ) * Rnd )
Loop

D = Euler(E, DN)

session("N")=N			' Product N
session("D")=D			' Private key


session("control")=1	' referral control

%>
<script Language="JavaScript">
function ModFunction(Main,Modulus)
{
	var ModFunction = Main - (Math.floor((Main/Modulus)) * Modulus);
	return ModFunction;
}

function Mult(x,p,m)
{
	var y = 1;
	var i = 0;
	
	for (i = p; i > 0; i--)
	{
		while ( (i/2) == (Math.floor((i/2))) )
		{	
			x = ModFunction((x * x), m);
			i = (i/2);
		}
		y = ModFunction((x * y), m);
	}
	
	return y;
}

function Encode(TheForm)
{
	// define variables
	var encoded_field = "";
	var original_field = TheForm.uName.value;
	var Enc = <%=E%>;
	var Mod = <%=N%>;
	
	// loop through all single letter of field
	for (i = 0; i <= original_field.length - 1; i++)
	{
		// charCodeAt gives the ASC value of character in position i
		encoded_field = encoded_field + Mult( (original_field.charCodeAt(i)) , Enc , Mod ) + ","
	}		
	
	TheForm.uName.value = "";
	TheForm.my_encoded_field.value = encoded_field;
}
</script>